﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class CreateEnemyInfo
{
    public float TimeStamp = 0;
    public int Count = 0;
    public CreateEnemyInfo(float timeStamp, int count)
    {
        this.TimeStamp = timeStamp;
        this.Count = count;
    }
}

public class GameLevelData
{
    public List<CreateEnemyInfo> EnemyInfo;
    public float ExistToWinTime;
}

public interface IGameManager {
    void GameOver(int result);
}

public interface IPlayerHero {
    void Live();
    void Dead();
    void SetGameManager(IGameManager mgr);
}

public class GameMgr : MonoBehaviour, ICreate,IGameManager
{

    public CanvasMgr CanvaMgr = null;

    private ActionProcesser ap_ = null;
    // Start is called before the first frame update
    void Start()
    {
        listOfRandHitUp_.Add("(任何时候都可以按[F1] 键重启游戏)");
        listOfRandHitUp_.Add("(按 [Tab] 键 可以切换武器哦)");
        listOfRandHitUp_.Add("(按 [J] 键 开火)");
        listOfRandHitUp_.Add("(按 [K] 键 跳跃)");
        listOfRandHitUp_.Add("(我的按键设计似乎暴漏了4399资深用户的身份 = =)");
        listOfRandHitUp_.Add("(英雄非常脆弱，碰到怪物就会死)");
        listOfRandHitUp_.Add("(生存够60秒将得到胜利！)");
        this.ap_ = this.GetComponent<ActionProcesser>();
        PrefabsMgr.GetInstance().Init(new PrefabsMgrInitConfigBase(100, 100, 0, 50, 50, 50, 50, 50, GameObject.Find("Players").transform));
        this.Player.SetGameManager(this);
        this.goLevel1();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab)) {
            if (this.Player != null) {
                this.Player.SwitchGun();
            }
        }
        this.tryTriggleLevel();
        this.updateScriptLogic();
    }


    // ----- 游戏过程 逻辑 -----

    private void updateScriptLogic()
    {
        if (nowScript_ != null)
        {
            var timeNow = Clock.GetInstance().FTime;
            var timeGameDurtion = timeNow - this.startGameTime_;
            this.tryProcessScript(timeGameDurtion);
            this.tryWin(timeGameDurtion);
        }
    }
    private void tryProcessScript(float gameDurtion)
    {

        if (nowScript_.EnemyInfo.Count > 0)
        {
            var item = nowScript_.EnemyInfo[0];
            if (Mathf.Abs(gameDurtion - item.TimeStamp) < 0.1)
            {
                var count = this.nowScript_.EnemyInfo[0].Count;
                this.nowScript_.EnemyInfo.RemoveAt(0);
                this.ap_.RunAction(new ActionCreateEnemy(this, count, 0.5f));
            }
        }
    }
    private void tryWin(float gameDurtion)
    {
        if (gameDurtion > this.nowScript_.ExistToWinTime)
        {
            Debug.Log("Win");
            this.nowScript_ = null;
            this.CanvaMgr.Show("胜利！", "按F1重来", this.getRandText(), 3);
            PrefabsMgr.GetInstance().RecycleType(PrefabsMgr.TYPE_IDIOT);
        }
    }
    // ----- 进入关卡 -----
    private void tryTriggleLevel() {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            this.goLevel1();
        }
        if (Input.GetKeyDown(KeyCode.F2))
        {
            this.goLevel2();
        }
        if (Input.GetKeyDown(KeyCode.F3))
        {
            this.goLevel3();
        }
    }
    private List<string> listOfRandHitUp_ = new List<string>();
    private string getRandText() {
        var count = listOfRandHitUp_.Count;
        if (count >0) {
            int rand = UnityEngine.Random.Range(0,count);
            string word = listOfRandHitUp_[rand];
            listOfRandHitUp_.RemoveAt(rand);
            return word;
        }
        return "";
    }
    private void goLevel1() {
        var script = new GameLevelData();
        script.EnemyInfo = new List<CreateEnemyInfo>();
        script.EnemyInfo.Add(new CreateEnemyInfo(1,5));
        script.EnemyInfo.Add(new CreateEnemyInfo(10,5));
        script.EnemyInfo.Add(new CreateEnemyInfo(20,7));
        script.EnemyInfo.Add(new CreateEnemyInfo(30,10));
        script.EnemyInfo.Add(new CreateEnemyInfo(40,10));
        script.EnemyInfo.Add(new CreateEnemyInfo(50,15));
        script.ExistToWinTime = 60;
        this.onGameStartByScript(script);
        this.CanvaMgr.Show(script.ExistToWinTime + "秒 生存","开始",this.getRandText(), 3);
    }
    private void goLevel2() {
        var script = new GameLevelData();
        script.EnemyInfo = new List<CreateEnemyInfo>();
        script.EnemyInfo.Add(new CreateEnemyInfo(1, 10));
        script.EnemyInfo.Add(new CreateEnemyInfo(20, 30));
        script.EnemyInfo.Add(new CreateEnemyInfo(40, 30));
        script.ExistToWinTime = 60;
        this.onGameStartByScript(script);
        this.CanvaMgr.Show(script.ExistToWinTime + "秒 生存", "开始", this.getRandText(), 3);
    }
    private void goLevel3() {
        var script = new GameLevelData();
        script.EnemyInfo = new List<CreateEnemyInfo>();
        script.EnemyInfo.Add(new CreateEnemyInfo(1, 10));
        script.EnemyInfo.Add(new CreateEnemyInfo(20, 30));
        script.EnemyInfo.Add(new CreateEnemyInfo(40, 30));
        script.ExistToWinTime = 60;
        this.onGameStartByScript(script);
        this.CanvaMgr.Show(script.ExistToWinTime + "秒 生存", "开始", this.getRandText(), 3);
    }


    private float startGameTime_ = 0;
    private GameLevelData nowScript_ = null;
    private void onGameStartByScript(GameLevelData script)
    {
        Time.timeScale = 1.0f;
        this.nowScript_ = script;
        this.startGameTime_ = Clock.GetInstance().FTime;

        // player Back toLife
        this.Player.Live();

        CameraMgr.GetInstance().CameraFollowTargetHoriPrefer(this.Player,1.5f);
        PrefabsMgr.GetInstance().RecycleType(PrefabsMgr.TYPE_IDIOT);
    }


    // ----- 实现接口 -----
    public void Create()
    {
        PrefabsMgr.GetInstance().Send(
               PrefabsMgr.TYPE_IDIOT,
               new WakeUpConfigIdiot(
                   new Vector2(-2, 5), 400
                   ));
    }

    public void GameOver(int result)
    {
        if (result == -1) {
            Time.timeScale = 0.4f;
            this.CancelInvoke();
            this.Invoke("onGameOverEnd", 1);
            // gameover
            this.nowScript_ = null;
            Debug.Log("Lose");
            this.CanvaMgr.Show("你死了", "按F1重来", this.getRandText(), 3);
        }
    }

    private void onGameOverEnd() {
         Time.timeScale = 1.0f;
    }

    public enum EALive
    {
        E_LIVE,
        E_DEAD,
    }
    public EALive LiveState = EALive.E_LIVE;


    // ----- 
    public Hero Player;
}
